package com.gift.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gift.auto.JSONAuthentication;
import com.gift.entity.GiProduct;
import com.gift.entity.GiUser;
import com.gift.entity.LpcType;
import com.gift.mapper.GiProductMapper;
import com.gift.response.Result;
import com.gift.utils.UpPhotoNameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gift
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/gi-product")
public class GiProductController extends JSONAuthentication {
    @Autowired
    GiProductMapper giProductMapper;
    @ApiOperation(value = "添加商品")
    @PostMapping("/add")
    public void addCar(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String  name) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",name);
        GiProduct baMeans1=giProductMapper.selectOne(wrapper);
        if(baMeans1!=null){
            Result error = Result.error().message("该商品名商品已存在,不可添加");
            this.WriteJSON(request, response, error);
        }else{
            GiProduct lpcType=new GiProduct();
            lpcType.setName(name);
            byte[] bytes = file.getBytes();
            String imageFileName = file.getOriginalFilename();
            String fileName = UpPhotoNameUtils.getPhotoName("img",imageFileName);
            Path path = Paths.get("D:\\自己的项目\\礼品册销售系统\\gif\\src\\assets\\picture\\" + fileName);
            //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
            Files.write(path, bytes);//写入文件
            String avatar_url=fileName;
            lpcType.setPicture(avatar_url);
            response.reset();
            if(giProductMapper.insert(lpcType)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }
    }
    @ApiOperation(value = "删除商品")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = giProductMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有商品")
    @PostMapping("/findAllPro")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<GiProduct> shUserPage=new Page<>(current,size);
        Page<GiProduct> shUserPages=giProductMapper.selectPage(shUserPage,null);
        List<GiProduct> records = shUserPages.getRecords();
        int total=giProductMapper.selectCount(null);

        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有商品通过商品名")
    @PostMapping("/findAllProByName")
    public void findAllProByName(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("name") String name)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("name",name);
        Page<GiProduct> shUserPage=new Page<>(current,size);
        Page<GiProduct> shUserPages=giProductMapper.selectPage(shUserPage,wrapper);
        List<GiProduct> records = shUserPages.getRecords();
        int total=giProductMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

